<?php
/**
 * 大客户信息类
 * @package  htdocs
 * @subpackage  module/base
 * @author   create by allen.qiang
 * @version 1.0
 */

class companyMaster{

    public function search( &$arg_array ) {

        //加载用户部门类
        quanli_require('module/base/system/sysDepartment.class.php');

        global $G_SHOP;


        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //print_r($arg_array);
        //公司编号
        if (isset($arg_array['company_id']) && is_numeric($arg_array['company_id'])) {
            $sql .= ' AND company_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['company_id'];
        }

        //公司名称
        if (isset($arg_array['company_name']) && !empty($arg_array['company_name'])) {
            $sql .= ' AND company_name LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['company_name']) . '%';
        }

        //联系人
        if (isset($arg_array['connect_name']) && !empty($arg_array['connect_name'])) {
            $sql .= ' AND connect_name LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['connect_name']) . '%';
        }

        //销售员ID
        if (isset($arg_array['userID']) && !empty($arg_array['userID'])) {
            $sql .= ' AND userID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['userID'];
        }

        //销售员姓名
        if (isset($arg_array['userName']) && !empty($arg_array['userName'])) {
            $sql .= ' AND userName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['userName']) . '%';
        }

        //部门ID
        if (isset($arg_array['departmentID']) && !empty($arg_array['departmentID']) && $arg_array['departmentID']!="") {
           /* $sql .= ' AND departmentID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['departmentID'];*/

            $sql .= ' AND departmentID in ('.$arg_array['departmentID'].')';

        }

        //部门名称
        if (isset($arg_array['departmentName']) && !empty($arg_array['departmentName'])) {
            $sql .= ' AND departmentName LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['departmentName']) . '%';
        }

        //省份
        if (isset($arg_array['province_name']) && !empty($arg_array['province_name'])) {
            $sql .= ' AND province_name = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['province_name']);
        }
        //市区
        if (isset($arg_array['city_name']) && !empty($arg_array['city_name'])) {
            $sql .= ' AND city_name = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['city_name'];
        }
        //地区
        if (isset($arg_array['country_name']) && !empty($arg_array['country_name'])) {
            $sql .= ' AND country_name = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['country_name'];
        }
        //省份id
        if (isset($arg_array['province_id']) && is_numeric($arg_array['province_id'])) {
            $sql .= ' AND province_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['province_id'];
        }

        //市区id
        if (isset($arg_array['city_id']) && is_numeric($arg_array['city_id'])) {
            $sql .= ' AND city_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['city_id'];
        }
        //地区id
        if (isset($arg_array['country_id']) && is_numeric($arg_array['country_id'])) {
            $sql .= ' AND country_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['country_id'];
        }

        //地区id
        if (isset($arg_array['departmentName']) && !empty($arg_array['departmentName'])) {
            $sql .= ' AND departmentName = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['departmentName'];
        }

        //地区id
        if (isset($arg_array['userName']) && !empty($arg_array['userName'])) {
            $sql .= ' AND userName = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['userName'];
        }

        //会员id
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //使用状态
        if (isset($arg_array['useFlg']) && in_array($arg_array['useFlg'], array(0, 1))) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //属性
        if (isset($arg_array['properties']) && $arg_array['properties']!="") {
            $sql .= ' AND properties = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['properties'];
        }

        //手机号
        if (isset($arg_array['mobile']) && !empty($arg_array['mobile'])) {
            $sql .= ' AND mobile = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['mobile'];
        }

        //绑定微信
        if (isset($arg_array['wx_openid']) && !empty($arg_array['wx_openid'])) {
            $sql .= ' AND wx_openid = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['wx_openid'];
        }

        //拓展字段
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            /*$sql .= $arg_array['extend'];
            $login_session =  $G_SHOP->sessionCache_get ( 'PS_LOGIN_SESSION' );
            $sql.="  OR departmentID=".$login_session['department']." AND userID='".$G_SHOP->currentUserID."')";*/

            $login_session =  $G_SHOP->sessionCache_get ( 'PS_LOGIN_SESSION' );
            $str_departmentID= strstr($arg_array['extend'],"(");
            $str_departmentID=str_replace('(',',',$str_departmentID) ;
            $str_departmentID=str_replace(')',',',$str_departmentID) ;

            if(strpos($str_departmentID,",".$login_session['department'].",")>-1){
                $str_departmentID=str_replace(','.$login_session['department'].',',',',$str_departmentID) ;
                $str_departmentID=trim($str_departmentID,',');
                if($str_departmentID==""){
                 //如果该用户所在的组和她目前大客户的组不在同一个大组，当前用户看不了她的大客户信息，只可以她的上级看 add  by loreta.cheng 2015-06-29 start
                 //实例化部门类
                 $sysDepartment = new sysDepartment();
                 //递归查询部门数据
                 $sysDepartment->searchDepartment($department_array,104);
                 $departmentIDold=$this->getCompanyDepartment( $G_SHOP->currentUserID );
                 if($sysDepartment->deep_in_array($login_session['department'],$department_array) && $departmentIDold  && $departmentIDold!=$login_session['department']){
                    $departmentResult=$sysDepartment->searchPdepartmentID($departmentIDold,$login_session['department'],$department_array);
                    if($departmentResult){
                      $sql.=" AND ( userID='".$G_SHOP->currentUserID."' OR (departmentID=".$login_session['department']." AND userID='".$G_SHOP->currentUserID."') )";
                    }else{
                      $sql.= $arg_array['extend'];
                    }
                 }else{
                   $sql.=" AND ( userID='".$G_SHOP->currentUserID."' OR (departmentID=".$login_session['department']." AND userID='".$G_SHOP->currentUserID."') )";
                  }
                 }else{
                  //如果该用户所在的组和她目前大客户的组不在同一个大组，当前用户看不了她的大客户信息，只可以她的上级看 add  by loreta.cheng 2015-06-29 end
                  $sql.=" AND ( userID='".$G_SHOP->currentUserID."' OR (departmentID IN ( ".$str_departmentID." ) OR departmentID=".$login_session['department']." ) )";
               }
            }else{
                $sql.= $arg_array['extend'];
            }
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM company_master ' . $sql;

                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM company_master ' . $sql;

            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'creator'     	=> $item->creator,
                    'createTime'  	=> $item->createTime,
                    'company_id'     => $item->company_id,
                    'company_name'   => $item->company_name,
                    'connect_name'   => $item->connect_name,
                    'province_name'   => $item->province_name,
                    'province_id'    	=> $item->province_id,
                    'city_name'  => $item->city_name,
                    'city_id'    	=> $item->city_id,
                    'country_name'       	=> $item->country_name,
                    'country_id'       	=> $item->country_id,
                    'address'       	=> $item->address,
                    'mobile'       	=> $item->mobile,
                    'website'       	=> $item->website,
                    'phone'       	=> $item->phone,
                    'employees_number_id'       	=> $item->employees_number_id,
                    'employees_number'       	=> $item->employees_number,
                    'industry_id'       	=> $item->industry_id,
                    'industry'       	=> $item->industry,
                    'nature_id'       	=> $item->nature_id,
                    'nature'       	=> $item->nature,
                    'note'       	=> $item->note,
                    'departmentID'       	=> $item->departmentID,
                    'departmentName'       	=> $item->departmentName,
                    'userID'       	=> $item->userID,
                    'userName'       	=> $item->userName,
                    'pcustID'       	=> $item->pcustID,
                    'useFlg'       	=> $item->useFlg,
                    'properties'      => $item->properties,
                    'wx_openid' => $item->wx_openid
                );

            }
            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    /**
     * 获取当前用户在大客户中的部门信息
     * @param $arg_array
     * @author allen.qiang@fistforward.com 2015-06-15
     */
    public function getCompanyDepartment ( $userID ) {
        global $G_SHOP;

        //检查参数
        if (!$userID) {
            return array( 'code' => 400, 'message' => '用户不存在' );
        }
        
        $company_masterSql = "select departmentID from company_master where userID = '".$userID."' LIMIT 1";
        $company_masterRe = $G_SHOP->DBCA->stmt_query($company_masterSql);
        if($company_masterRe){
            return $company_masterRe[0]->departmentID;
        }else{
           return null;
        }
    }


    /**
     * 修改某个用户对应的大客户的部门信息
     * @param $arg_array
     * @author allen.qiang@fistforward.com 2015-06-15
     */
    public function changeCompanyDepartment ( $arg_array ) {
        global $G_SHOP;

        //检查参数
        if (empty($arg_array['userID'])) {
            return array( 'code' => 400, 'message' => '用户不存在' );
        }
        if (empty($arg_array['departmentID'])) {
            return array( 'code' => 400, 'message' => '部门id不存在' );
        }
        if (empty($arg_array['departmentName'])) {
            return array( 'code' => 400, 'message' => '部门名称不存在' );
        }
        
      //查询该用户下是否存在大用户信息
        $com_array=array(
            'userID' => $arg_array['userID'],
            'departmentID' => $arg_array['departmentIDold'],
            'sortname' => 'createTime',
            'sortorder' => 'ASC',
            'page' => '1',
            'rp' => '1'
            );
        $com_result=$this->search($com_array);
        if(!empty($com_result)){
            $company_masterSql = "UPDATE company_master SET departmentID = '".$arg_array['departmentID']."',departmentName = '".$arg_array['departmentName']."' WHERE userID = ".$arg_array['userID']."'";
            $company_masterRe = $G_SHOP->DBCA->stmt_query($company_masterSql);
            return true;
        }else{
           return false;
        }
    }

    /**
     * 添加、修改大客户信息
     *
     */
    public function editCompany ( $arg_array ) {

        global $G_SHOP;

        //检查参数
        if (!isset( $arg_array['company_name'], $arg_array['address'], $arg_array['mobile'],
            $arg_array['connect_name'], $arg_array['departmentID'],$arg_array['note'],  $arg_array['userID'],$arg_array['province_id'],
            $arg_array['city_id'], $arg_array['country_id'])){
            return array('message'=>'参数错误','code'=> 400);
        }

        if (empty($arg_array['company_name'])) {
            return array( 'code' => 400, 'message' => '公司名称不能为空' );
        }

        if (empty($arg_array['connect_name'])) {
            return array( 'code' => 400, 'message' => '联系人不能为空' );
        }

        if (empty($arg_array['phone']) && empty($arg_array['mobile'])) {
            return array( 'code' => 400, 'message' => '联系电话和联系手机必须填写一项' );
        }

        if (empty($arg_array['province_id'])) {
            return array( 'code' => 400, 'message' => '请选择省份' );
        }

        if (empty($arg_array['city_id'])) {
            return array( 'code' => 400, 'message' => '请选择市区' );
        }

        if (empty($arg_array['country_id'])) {
            return array( 'code' => 400, 'message' => '请选择地区' );
        }

        if (empty($arg_array['address'])) {
            return array( 'code' => 400, 'message' => '地址不能为空' );
        }

        if (empty($arg_array['departmentID'])) {
            return array( 'code' => 400, 'message' => '请选择部门' );
        }

        if (empty($arg_array['userID'])) {
            return array( 'code' => 400, 'message' => '请选择销售员' );
        }

        if (!isset($args_array['company_id']) || !quanli_is_int($args_array['company_id'])) {
            $args_array['company_id'] = 0;
        }

        quanli_include('module/base/system/sysUser.class.php');
        quanli_include('module/base/customer/quanliCust.class.php');
        quanli_include('module/base/customer/custDeliver.class.php');

        //实例用户类
        $sysUser = new sysUser($G_SHOP->DBCA);

        //实例会员类
        $quanliCust = new quanliCust();

        //实例会员地址类
        $custDeliver = new custDeliver();


        $creator                          = $arg_array['creator'];
        $company_id                       = $arg_array['company_id'];
        $company_name                     = $arg_array['company_name'];
        $connect_name	                  = $arg_array['connect_name'];
        $province_id                      = $arg_array['province_id'];
        $province_name                    = $arg_array['province_name'];
        $city_id                          = $arg_array['city_id'];
        $city_name                        = $arg_array['city_name'];
        $country_id                       = $arg_array['country_id'];
        $country_name                     = $arg_array['country_name'];
        $address                          = $arg_array['address'];
        $mobile                           = $arg_array['mobile'];
        //$website                          = $arg_array['website'];
        //$phone                            = $arg_array['phone'];
        //$employees_number_id              = $arg_array['employees_number_id'];
        //$employees_number                 = $arg_array['employees_number'];
        //$industry_id                      = $arg_array['industry_id'];
        //$industry                         = $arg_array['industry'];
        //$nature_id                        = $arg_array['nature_id'];
        //$nature                           = $arg_array['nature'];
        $note                             = $arg_array['note'];
        $userID                           = $arg_array['userID'];
        $departmentID                     = $arg_array['departmentID'];
        $departmentName                   = $arg_array['departmentName'];
        $properties                       = $arg_array['properties'];//属性

        try {

            //查询用户信息
            $search_args_array = array(
                'userID' => $userID
            );
            $user_array = $sysUser->searchUserInfo( $search_args_array );

            if(empty($user_array[0]['mobile'])){
                throw new Exception('销售员手机号为空', 400);
            }

            $company_master = new entity('company_master') ;
            if ($company_id == 0) {
                if ($G_SHOP->DBCA->stmt_query('SELECT * FROM company_master WHERE company_name = ?', 's', $company_name)) {
                    throw new Exception('大客户名称重复', 400);
                }

                if ($G_SHOP->DBCA->stmt_query('SELECT * FROM company_master WHERE mobile = ?', 's', $arg_array['mobile'])) {
                    throw new Exception('手机号已存在', 400);
                }

                $company_master->creator = $creator;
                $company_master->useFlg = 1;
                $company_master->company_id = generate_seqno('seqno_copany');
                $company_master->company_name = $company_name;

                $args_array = array(
                    'operator' => $creator,
                    'pcustID' => 0,
                    'custID' => $company_master->company_id . get_random_string(6),
                    'nickName' => $connect_name,
                    'custname' => $connect_name,
                    'custType' => 4,
                    'sexy' => null,
                    'mobile' => $mobile,
                    'custSource' => 1
                );
                $res = $quanliCust->editCustomer( $args_array );
                if ( $res['code'] == 200 ) {
                    $pcustID = $res['data']['pcustID'];
                    $company_master->pcustID = $pcustID;
                } else {
                    throw new Exception($res['message'], 400);
                }
                //修改大客户默认地址添加地址 add loreta.cheng 2015-06-09 start
                //$deliverID=0;
                //修改大客户默认地址添加地址 add loreta.cheng 2015-06-09 end
            } else {
                $company_master->company_id = $company_id;

                //修改大客户默认地址添加地址 add loreta.cheng 2015-06-09 start
//                $com_array=array(
//                    'company_id' => $company_id,
//                    'sortname' => 'createTime',
//                    'sortorder' => 'ASC',
//                    'page' => '1',
//                    'rp' => '1'
//                );
//                $com_result=$this->search($com_array);
//                $cust_info=$custDeliver->getBoldAddress( $com_result[0]['pcustID'] );
//                $deliverID=$cust_info['deliverID'];
//                $pcustID=$com_result[0]['pcustID'];
                //修改大客户默认地址添加地址 add loreta.cheng 2015-06-09 end
                if ($G_SHOP->DBCA->stmt_query('SELECT * FROM company_master WHERE company_id <> ? AND mobile = ?', 'is', array($company_id, $arg_array['mobile']))) {
                    throw new Exception('手机号已存在', 400);
                }

                if (!$G_SHOP->DBCA->fetch($company_master)) {
                    throw new Exception('大客户信息没有找到', 404);
                }
            }

            //修改大客户默认地址添加地址 add loreta.cheng 2015-06-09 start
            //保存地址信息 
//            $arg_array = array(
//                'pcustID' => $pcustID,
//                'creator' => $creator,
//                'deliverID' => $deliverID,
//                'custName' => $user_array[0]['trueName'],
//                'phone' => $phone,
//                'mobile' => $user_array[0]['mobile'],
//                'address' => $address,
//                'provinceid' => $province_id,
//                'cityid' => $city_id,
//                'countyid' => $country_id,
//            );
//            $result_array = $custDeliver->editCustDeliver( $arg_array );
//            if ($result_array['code'] != 200) {
//                throw new Exception($result_array['message'], $result_array['code']);
//            }
            //修改大客户默认地址添加地址 add loreta.cheng 2015-06-09 end

            //保存信息
            $company_master->connect_name = $connect_name;
            $company_master->mobile = $mobile;
            $company_master->province_name = $province_name;
            $company_master->province_id = $province_id;
            $company_master->city_name = $city_name;
            $company_master->city_id = $city_id;
            $company_master->country_name = $country_name;
            $company_master->country_id = $country_id;
            $company_master->address = $address;
            $company_master->note = $note;
            $company_master->properties = $properties;
            $company_master->userID = $userID  ;
            $company_master->userName = $user_array[0]['trueName'];
            $company_master->departmentID = $departmentID;
            $company_master->departmentName = $departmentName;
            /*
            $company_master->website = $website;
            $company_master->phone = $phone;
            $company_master->employees_number_id = $employees_number_id;
            $company_master->industry_id = $industry_id;
            $company_master->nature_id = $nature_id;
            $company_master->industry_id = $industry_id;
            $company_master->departmentID = isset($arg_array['departmentID']) ? $arg_array['departmentID'] : "" ;
            $company_master->departmentName = isset($arg_array['departmentName']) ? $arg_array['departmentName'] : "";
            $company_master->employees_number = $employees_number;
            $company_master->nature = $industry;
            $company_master->industry = $nature;
            */
            $G_SHOP->DBCA->save( $company_master ) ;

            return array( 'code' => 200, 'message' => '大客户保存成功' );

        } catch (Exception $e) {
            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }
    }


    /**
     * 删除大客户信息方式
     * @param array $pay_array 配送站ID数组
     * @return array
     */
    public function deleteCompany ( $pay_array ) {

        global $G_SHOP;

        if (!isset($pay_array) || !is_array($pay_array) || empty($pay_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            $G_SHOP->DBCA->beginTransaction();

            foreach($pay_array as &$company_id) {



                //查询配送站方式
                $pay_obj = new entity('company_master');
                $pay_obj->company_id = $company_id;
                if (!$G_SHOP->DBCA->fetch($pay_obj)) {
                    throw new Exception('大客户信息没有找到', 404);
                }

                //删除大客户信息
                $G_SHOP->DBCA->delete($pay_obj);

                //删除删除大客户映射信息
                $delete_sql = 'DELETE FROM  company_master WHERE company_id = ? ';
                $G_SHOP->DBCA->stmt_query($delete_sql, 'i', $company_id);

            }

            $G_SHOP->DBCA->commit();

            return array( 'code' => 200, 'message' => '删除大客户信息成功' );

        } catch ( Exception $e ) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

    /**
     * 大客户信息 启用/禁用
     * @param $arg_array
     * @return array
     */
    public function changeCompanyUseflg ( $arg_array ) {

        global $G_SHOP;

        if (!isset($arg_array['company_id'], $arg_array['flg']) || !quanli_is_int($arg_array['company_id']) || !in_array($arg_array['flg'], array(0,1)))
            return array( 'code' => 400, 'message' => '参数错误' );

        try {

            //查询配送员
            $company_master_obj = new entity('company_master');
            $company_master_obj->company_id = $arg_array['company_id'];
            if (!$G_SHOP->DBCA->fetch($company_master_obj))
                return array( 'code' => 404, 'message' => '大客户信息信息没有找到' );

            if ($arg_array['flg']) {

                if ($company_master_obj->useFlg == 0) {
                    return array( 'code' => 406, 'message' => '大客户信息已禁用' );
                }
                $company_master_obj->useFlg = 0;

            } else {

                if ($company_master_obj->useFlg == 1) {
                    return array( 'code' => 406, 'message' => '大客户信息已启用' );
                }
                $company_master_obj->useFlg = 1;

            }
            $G_SHOP->DBCA->save($company_master_obj);

            if ($arg_array['flg'])
                return array( 'code' => 200, 'message' => '大客户信息禁用成功' );
            else
                return array( 'code' => 200, 'message' => '大客户信息启用成功' );

        } catch (Exception $e) {
            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }
        }

    }

    //绑定微信openId
    public function bindOpenId ( $id, $openId )
    {
        global $G_SHOP;
        $company = new entity('company_master');
        $company->company_id = $id;
        if (!$G_SHOP->DBCA->fetch($company))
            return array( 'code' => 404, 'message' => '大客户信息信息没有找到' );

        $company->wx_openid = $openId;
        $G_SHOP->DBCA->save($company);
        return array( 'code' => 200, 'message' => '' );
    }

    public function searchRecord (&$arg_array){

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //公司名称
        if (isset($arg_array['company_name']) && !empty($arg_array['company_name'])) {
            $sql .= ' AND company_name LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['company_name']) . '%';
        }
        //联系人
        if (isset($arg_array['contacts']) && !empty($arg_array['contacts'])) {
            $sql .= ' AND cr.contacts LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['contacts']) . '%';
        }

        //销售员ID
        if (isset($arg_array['userID']) && !empty($arg_array['userID'])) {
            $sql .= ' AND cm.userID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['userID'];
        }

        //部门ID
        if (isset($arg_array['departmentID']) && !empty($arg_array['departmentID']) && $arg_array['departmentID']!="") {
            $sql .= ' AND cm.departmentID in ('.$arg_array['departmentID'].')';
        }

        //拓展字段
        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM company_record cr
                              INNER JOIN company_master cm ON cr.companyId=cm.company_id ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                //排序
                $sql .= ' ORDER BY createTime DESC ';

                $arg_array['count'] = $item_count;
                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            }

            $search_sql = 'SELECT cr.*,cm.company_name,um.trueName FROM company_record cr
                          INNER JOIN company_master cm ON cr.companyId=cm.company_id
                          LEFT JOIN user_master um ON um.userID = cr.creator ' . $sql;

            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)return null;

            return $temp_item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }
    }

    //保存拜访记录
    public function saveRecord( $arg_array ){
        global $G_SHOP;
        $record = new entity('company_record');
        $record->creator = $arg_array['creator'];
        $record->companyId = $arg_array['company'];
        $record->recordDate = $arg_array['recordDate'];
        $record->contacts = $arg_array['contacts'];
        $record->mobile = $arg_array['mobile'];
        $record->address = $arg_array['address'];
        $record->content = $arg_array['content'];
        $record->feedback = $arg_array['feedback'];
        $record->opinion = $arg_array['opinion'];

        $G_SHOP->DBCA->save($record);
        return array( 'code' => 200);
    }
}
